var mongoose = require('mongoose');
//查询数据
const getall = async () => await mongoose.model("supplierfoodsModle").find();
   
//查询图片
const getById = async ({ _id }) => {
    return await mongoose.model("supplierfoodsModle")
        .findOne({ _id })
        .populate("className")
        .exec();
}
//更新
const update = async (_id, params) => await mongoose.model("supplierfoodsModle").update({ _id }, params);

//新增
const create = async supplierfoods => await mongoose.model("supplierfoodsModle").create(supplierfoods);

//删除
const remove = async (_id) => await mongoose.model("supplierfoodsModle").remove(_id);

//分页
const get = async ({ curPage, eachPage, nameData,sort }) => {
    const result = {};
    const supplierfoodsModle = mongoose.model("supplierfoodsModle");
    result.total = await supplierfoodsModle.find(nameData);
    result.total = result.total.length;
    result.rows = await supplierfoodsModle
        .find(nameData)
        .skip((curPage - 1) * eachPage)//跳过
        .limit(eachPage)//限制
        .sort({ _id: sort })//排序
        .exec();
    result.maxPage = Math.ceil(result.total / eachPage);
    return result;
};
module.exports = { get, update, remove, create, getById ,getall};
